#include "DB_MySql.h"


/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
MySql_DB::MySql_DB(string v_strServer,string v_strUser,string v_strPassword )
{
      m_strServer    = v_strServer;
      m_strUser      = v_strUser;
      m_strPassword  = v_strPassword;
}
/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
bool MySql_DB::ConnectToServer(string v_strDataBase)
{
     if( mysql_init(&m_sql) == NULL )
       {
        fprintf(stderr, "%s\n", mysql_error(&m_sql));
        LOG(ERROR) << mysql_error(&m_sql) ;
        return false;
       }
      if(mysql_set_character_set(&m_sql,"UTF8") != 0)
      {
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
         return false;
      }
       my_bool reconnect = 0;
       mysql_options(&m_sql, MYSQL_OPT_RECONNECT, &reconnect);
       if (!mysql_real_connect(&m_sql, m_strServer.c_str(),m_strUser.c_str(), m_strPassword.c_str(), v_strDataBase.c_str(), 0, NULL, 0))
      {
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
         return false;
      }


    LOG(INFO) << "connect to mysql server database :" << v_strDataBase;
//    InsertToTableUser("zhangbing","abc","3344");
//    JudgeLogin("zhangbing","abcn");
    return true;
}
/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
bool MySql_DB::InsertToTableUser(string v_strUserName ,
                                 string v_strPassword ,
                                 string v_strDeviceID)
{
 string str_Sql =  "insert into User values (";

        str_Sql += "NULL";
        str_Sql += ",";

        str_Sql += "'";
        str_Sql += v_strUserName;
        str_Sql += "'";
        str_Sql += ",";

        str_Sql += "'";
        str_Sql += v_strPassword;
        str_Sql += "'";
        str_Sql += ",";

        str_Sql += "'";
        str_Sql += v_strDeviceID;
        str_Sql += "'";

        str_Sql += ");";

    mysql_ping(&m_sql);

 if(mysql_query(&m_sql, str_Sql.c_str()))        //执行SQL语句
    {
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
         return false;
    }
    return true;
}
/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
int MySql_DB::JudgeLogin(string v_strUserName ,string v_strPassword )
{
  string str_Sql =  "select * from TableUser ";

        str_Sql += "where UserName = ";

        str_Sql += "'";
        str_Sql += v_strUserName;
        str_Sql += "'";

        str_Sql += " AND UserPassword = ";
        str_Sql += "'";
        str_Sql += v_strPassword;
        str_Sql += "';";
    mysql_ping(&m_sql);
    if(mysql_query(&m_sql, str_Sql.c_str()))        //执行SQL语句
    {
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
         return 0;
    }

    //获取结果集
    if (!(m_pRes = mysql_store_result(&m_sql)))    //获得sql语句结束后返回的结果集
    {
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
         return 0;
    }
    if(mysql_affected_rows(&m_sql) < 1)
    {
      LOG(INFO) << "UserName Or Password Error";
      return 0;
    }
    string strId = "";
    m_Row = mysql_fetch_row(m_pRes);
    strId = m_Row[0];
    return atoi(strId.c_str());
}
/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
int MySql_DB::InsertLoginTimeAndGetId(int v_nUserId,string v_strLoginTime )
{

 string str_Sql =  "insert into TableLoginInformation(UserId,LoginTime) values (";

        str_Sql += IntToString(v_nUserId);
        str_Sql += ",";

        str_Sql += "'";
        str_Sql += v_strLoginTime;
        str_Sql += "'";

        str_Sql += ");";

    mysql_ping(&m_sql);

 if(mysql_query(&m_sql, str_Sql.c_str()))        //执行SQL语句
    {
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
         return 0;
    }
    str_Sql = "select last_insert_id();";

    mysql_ping(&m_sql);

 if(mysql_query(&m_sql, str_Sql.c_str()))        //执行SQL语句
    {
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
         return 0;
    }
     //获取结果集
    if (!(m_pRes = mysql_store_result(&m_sql)))    //获得sql语句结束后返回的结果集
    {
         LOG(ERROR) << str_Sql ;
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
         return 0;
    }

    string strId = "";
    m_Row = mysql_fetch_row(m_pRes);
    strId = m_Row[0];
    return atoi(strId.c_str());

}

/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
bool MySql_DB::SetLogoutTimeById(int v_Id,string v_strLogoutTime)
{
   string str_Sql =  "update TableLoginInformation set LogoutTime = ";

        str_Sql += "'" + v_strLogoutTime + "'";

        str_Sql += " where ID = ";

        str_Sql += IntToString(v_Id);

        str_Sql += ";";

     mysql_ping(&m_sql);

    if(mysql_query(&m_sql, str_Sql.c_str()))        //执行SQL语句
    {
         LOG(ERROR) << str_Sql ;
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
         return false;
    }
    return true;
}

/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
bool MySql_DB::SetDeviceStateById(int v_Id,string v_strState)
{
   string str_Sql =  "update TableDevice set DeviceState = ";

        str_Sql +=  v_strState ;

        str_Sql += " where ID = ";

        str_Sql += IntToString(v_Id);

        str_Sql += ";";

    mysql_ping(&m_sql);

    if(mysql_query(&m_sql, str_Sql.c_str()))        //执行SQL语句
    {
         LOG(ERROR) << str_Sql ;
         fprintf(stderr, "%s\n", mysql_error(&m_sql));
         LOG(ERROR) << mysql_error(&m_sql) ;
         return false;
    }
    return true;
}
/****************************
Function   :
Parameter  :
ReturnValue:
Author     :
Data       :
****************************/
MySql_DB::~MySql_DB()
{
    mysql_free_result(m_pRes);
    mysql_close(&m_sql);
}

